import '../model/like_model.dart';

/// 点赞默认数据
/// 提供默认的帖子点赞数据
class LikeData {
  /// 获取所有默认点赞数据
  static List<LikeModel> getDefaultLikes() {
    final now = DateTime.now();

    return [
      // Post 1 的点赞
      LikeModel(
        id: '1',
        userId: '2', // gardenmaster
        postId: '1',
        likeTime: now.subtract(const Duration(days: 4, hours: 1)),
      ),
      LikeModel(
        id: '2',
        userId: '3', // leafyfriend
        postId: '1',
        likeTime: now.subtract(const Duration(days: 3, hours: 4)),
      ),
      LikeModel(
        id: '3',
        userId: '4', // plantcare
        postId: '1',
        likeTime: now.subtract(const Duration(days: 2, hours: 7)),
      ),
      LikeModel(
        id: '4',
        userId: '5', // greensoul
        postId: '1',
        likeTime: now.subtract(const Duration(days: 1, hours: 12)),
      ),
      LikeModel(
        id: '5',
        userId: '6', // blooming
        postId: '1',
        likeTime: now.subtract(const Duration(hours: 18)),
      ),

      // Post 2 的点赞
      LikeModel(
        id: '6',
        userId: '1', // admin
        postId: '2',
        likeTime: now.subtract(const Duration(days: 2, hours: 2)),
      ),
      LikeModel(
        id: '7',
        userId: '3', // leafyfriend
        postId: '2',
        likeTime: now.subtract(const Duration(days: 1, hours: 8)),
      ),
      LikeModel(
        id: '8',
        userId: '4', // plantcare
        postId: '2',
        likeTime: now.subtract(const Duration(days: 1, hours: 3)),
      ),
      LikeModel(
        id: '9',
        userId: '5', // greensoul
        postId: '2',
        likeTime: now.subtract(const Duration(hours: 20)),
      ),
      LikeModel(
        id: '10',
        userId: '6', // blooming
        postId: '2',
        likeTime: now.subtract(const Duration(hours: 16)),
      ),
      LikeModel(
        id: '11',
        userId: '7', // plantlife
        postId: '2',
        likeTime: now.subtract(const Duration(hours: 12)),
      ),
      LikeModel(
        id: '12',
        userId: '8', // naturelover
        postId: '2',
        likeTime: now.subtract(const Duration(hours: 8)),
      ),

      // Post 3 的点赞
      LikeModel(
        id: '13',
        userId: '1', // admin
        postId: '3',
        likeTime: now.subtract(const Duration(days: 1, hours: 5)),
      ),
      LikeModel(
        id: '14',
        userId: '2', // gardenmaster
        postId: '3',
        likeTime: now.subtract(const Duration(hours: 22)),
      ),
      LikeModel(
        id: '15',
        userId: '4', // plantcare
        postId: '3',
        likeTime: now.subtract(const Duration(hours: 18)),
      ),
      LikeModel(
        id: '16',
        userId: '5', // greensoul
        postId: '3',
        likeTime: now.subtract(const Duration(hours: 14)),
      ),
      LikeModel(
        id: '17',
        userId: '7', // plantlife
        postId: '3',
        likeTime: now.subtract(const Duration(hours: 10)),
      ),

      // Post 4 的点赞
      LikeModel(
        id: '18',
        userId: '1', // admin
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 19)),
      ),
      LikeModel(
        id: '19',
        userId: '2', // gardenmaster
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 15)),
      ),
      LikeModel(
        id: '20',
        userId: '3', // leafyfriend
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 11)),
      ),
      LikeModel(
        id: '21',
        userId: '5', // greensoul
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 7)),
      ),
      LikeModel(
        id: '22',
        userId: '6', // blooming
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 3)),
      ),
      LikeModel(
        id: '23',
        userId: '8', // naturelover
        postId: '4',
        likeTime: now.subtract(const Duration(hours: 1)),
      ),

      // Post 5 的点赞
      LikeModel(
        id: '24',
        userId: '1', // admin
        postId: '5',
        likeTime: now.subtract(const Duration(hours: 10)),
      ),
      LikeModel(
        id: '25',
        userId: '2', // gardenmaster
        postId: '5',
        likeTime: now.subtract(const Duration(hours: 6)),
      ),
      LikeModel(
        id: '26',
        userId: '4', // plantcare
        postId: '5',
        likeTime: now.subtract(const Duration(hours: 2)),
      ),

      // Post 6 的点赞
      LikeModel(
        id: '27',
        userId: '1', // admin
        postId: '6',
        likeTime: now.subtract(const Duration(hours: 7)),
      ),
      LikeModel(
        id: '28',
        userId: '2', // gardenmaster
        postId: '6',
        likeTime: now.subtract(const Duration(hours: 5)),
      ),
      LikeModel(
        id: '29',
        userId: '3', // leafyfriend
        postId: '6',
        likeTime: now.subtract(const Duration(hours: 3)),
      ),
      LikeModel(
        id: '30',
        userId: '4', // plantcare
        postId: '6',
        likeTime: now.subtract(const Duration(hours: 1)),
      ),
      LikeModel(
        id: '31',
        userId: '5', // greensoul
        postId: '6',
        likeTime: now.subtract(const Duration(minutes: 45)),
      ),
      LikeModel(
        id: '32',
        userId: '7', // plantlife
        postId: '6',
        likeTime: now.subtract(const Duration(minutes: 30)),
      ),
      LikeModel(
        id: '33',
        userId: '8', // naturelover
        postId: '6',
        likeTime: now.subtract(const Duration(minutes: 15)),
      ),

      // Post 7 的点赞
      LikeModel(
        id: '34',
        userId: '1', // admin
        postId: '7',
        likeTime: now.subtract(const Duration(hours: 3)),
      ),
      LikeModel(
        id: '35',
        userId: '2', // gardenmaster
        postId: '7',
        likeTime: now.subtract(const Duration(hours: 1, minutes: 30)),
      ),
      LikeModel(
        id: '36',
        userId: '3', // leafyfriend
        postId: '7',
        likeTime: now.subtract(const Duration(minutes: 45)),
      ),
      LikeModel(
        id: '37',
        userId: '5', // greensoul
        postId: '7',
        likeTime: now.subtract(const Duration(minutes: 20)),
      ),
      LikeModel(
        id: '38',
        userId: '6', // blooming
        postId: '7',
        likeTime: now.subtract(const Duration(minutes: 10)),
      ),

      // Post 8 的点赞
      LikeModel(
        id: '39',
        userId: '1', // admin
        postId: '8',
        likeTime: now.subtract(const Duration(hours: 1, minutes: 45)),
      ),
      LikeModel(
        id: '40',
        userId: '2', // gardenmaster
        postId: '8',
        likeTime: now.subtract(const Duration(hours: 1, minutes: 15)),
      ),
      LikeModel(
        id: '41',
        userId: '3', // leafyfriend
        postId: '8',
        likeTime: now.subtract(const Duration(minutes: 30)),
      ),
      LikeModel(
        id: '42',
        userId: '4', // plantcare
        postId: '8',
        likeTime: now.subtract(const Duration(minutes: 15)),
      ),
      LikeModel(
        id: '43',
        userId: '6', // blooming
        postId: '8',
        likeTime: now.subtract(const Duration(minutes: 5)),
      ),
    ];
  }

  /// 根据帖子ID获取点赞
  static List<LikeModel> getLikesByPostId(String postId) {
    return getDefaultLikes().where((like) => like.postId == postId).toList()
      ..sort((a, b) => b.likeTime.compareTo(a.likeTime));
  }

  /// 根据用户ID获取点赞
  static List<LikeModel> getLikesByUserId(String userId) {
    return getDefaultLikes().where((like) => like.userId == userId).toList()
      ..sort((a, b) => b.likeTime.compareTo(a.likeTime));
  }

  /// 获取帖子的点赞数量
  static int getPostLikeCount(String postId) {
    return getDefaultLikes().where((like) => like.postId == postId).length;
  }

  /// 获取用户的总点赞数
  static int getUserTotalLikes(String userId) {
    return getDefaultLikes().where((like) => like.userId == userId).length;
  }

  /// 获取点赞统计信息
  static Map<String, int> getLikeStatistics() {
    final likes = getDefaultLikes();
    final stats = <String, int>{};

    // 总点赞数
    stats['totalLikes'] = likes.length;

    // 各帖子的点赞数
    for (int i = 1; i <= 8; i++) {
      stats['post${i}Likes'] = getPostLikeCount(i.toString());
    }

    // 各用户的点赞数
    for (int i = 1; i <= 8; i++) {
      stats['user${i}Likes'] = getUserTotalLikes(i.toString());
    }

    return stats;
  }

  /// 根据ID获取点赞
  static LikeModel? getLikeById(String id) {
    try {
      return getDefaultLikes().firstWhere((like) => like.id == id);
    } catch (e) {
      return null;
    }
  }

  /// 检查用户是否点赞了帖子
  static bool isPostLikedByUser(String postId, String userId) {
    return getDefaultLikes().any(
      (like) => like.postId == postId && like.userId == userId,
    );
  }

  /// 获取最受欢迎的帖子（按点赞数排序）
  static List<String> getMostLikedPostIds() {
    final postLikes = <String, int>{};

    // 统计每个帖子的点赞数
    for (int i = 1; i <= 8; i++) {
      postLikes[i.toString()] = getPostLikeCount(i.toString());
    }

    // 按点赞数排序
    final sortedPosts =
        postLikes.entries.toList()..sort((a, b) => b.value.compareTo(a.value));

    return sortedPosts.map((entry) => entry.key).toList();
  }
}
